package controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import pojo.News;
import pojo.User;
import service.NewsService;
import service.UserService;
import utils.APage;

@Controller
@RequestMapping("/user")
public class UserController {

	@Resource
	private UserService userService;
	@Resource
	private NewsService newsService;
	
	@RequestMapping(value = "/goRegist.html")
	public String goRegist() {
		return "Regist";
	}
	@RequestMapping(value = "/goLogin.html")
	public String goLogin() {
		return "Login";
	}
	@RequestMapping(value = "/goUserManager.html")
	public String goUserManager(
		HttpSession session,
		Map<String, Object> map,
		// 当前页,请求页
		@RequestParam(value = "cp", required = false, defaultValue = "1") int currentPage,
		// 前端
		@RequestParam(value = "unit", required = false) String unit,
		@RequestParam(value = "title", required = false) String title,
		@RequestParam(value = "startTime", required = false) String startTime,
		@RequestParam(value = "endTime", required = false) String endTime
		) {
		User user = (User)session.getAttribute("u");
		int uId= user.getId();
		int pageSize = 5;
		List<News> list = newsService.selectNewsList(unit, title, startTime, endTime,uId);
		int pageCount = newsService.getPageCount(list.size(), pageSize);
		if (currentPage < 0) {
			currentPage = 1;
		}
		if (currentPage > pageCount){
			currentPage = pageCount;
		}
		List<News> nl = APage.collSetPagingPojo(currentPage, pageSize, list);

		map.put("nl", nl);
		map.put("pc", pageCount);
		map.put("cp", currentPage);
		map.put("unit", unit);
		map.put("title", title);
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		return "UserManager";
	}

	@RequestMapping(value = "/addUser", method = RequestMethod.POST)
	public String addUser(
			User user,
			HttpServletRequest req
			) {
		try {
			int result = userService.add(user);
			if (result > -1) {
				return "redirect:goLogin.html";
			}
			req.setAttribute("registError","注册失败！请重试或拨打客服热线");
			return "Regist";
		} catch (Exception e) {
			// TODO: handle exception
			req.setAttribute("registError","注册失败！用户名重复");
			return "Regist";			
		}
	}
	
	@RequestMapping(value = "/validUser", method = RequestMethod.POST)
	public String validUser(
			@RequestParam(value = "loginName", required = false)String loginName,
			@RequestParam(value = "password", required = false)String password,
			HttpServletRequest req,
			HttpSession session
			) {
		if( loginName==null || loginName.equals("")){
			req.setAttribute("loginError","请输入用户名");
			return "Login";
		}
		User user = userService.isExistUser(loginName, password);
		try {
			if (user != null) {
				user.setPassword(null);
				session.setAttribute("u", user);
				return "redirect:../news/goNewsItems.html";
			}
			req.setAttribute("loginError","用户名或密码错误！");
			return "Login";
		} catch (Exception e) {
			// TODO: handle exception
			req.setAttribute("loginError","登陆失败!,请联系管理员");
			return "Login";
		}
	}
}
